package com.tian.core.aop;

import com.tian.core.beans.ResultBean;
import com.tian.core.exceptions.CheckException;
import com.tian.core.exceptions.NoPermissionException;
import com.tian.core.exceptions.UnloginException;
import com.tian.core.lang.Constant;
import com.tian.core.lang.ErrorDefine;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
 * @author YangTao
 * @date 2025/4/18
 */
@Aspect
@Component
public class ControllerAOP {
    private static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);

    @Around("execution(public com.tian.core.beans.ResultBean *(..))")
    public Object handlerControllerMethod(ProceedingJoinPoint pjp) {
        long startTime = System.currentTimeMillis();
        ResultBean<?> result = null;
        try {
            result = (ResultBean<?>) pjp.proceed();
            logger.info(pjp.getSignature() + "use time:" + (System.currentTimeMillis() - startTime));
        } catch (Throwable e) {
            result = handlerException(pjp, e);
        }
        return result;
    }
    private ResultBean<?> handlerException(ProceedingJoinPoint pjp, Throwable e) {
        ResultBean<?> result = new ResultBean<>();
        // 已知异常
        // 校验出错，参数非法
        if (e instanceof CheckException || e instanceof IllegalArgumentException) {
            result.setRespMsg(e.getLocalizedMessage());
            result.setRespCode(ErrorDefine.CHECK_FAIL);
        }
        // 没有登陆
        else if (e instanceof UnloginException) {
            result.setRespMsg(ErrorDefine.NO_LOGIN_MSG);
            result.setRespCode(ErrorDefine.NO_LOGIN);
        }
        // 没有权限
        else if (e instanceof NoPermissionException) {
            result.setRespMsg(ErrorDefine.NO_PERMISSION_MSG);
            result.setRespCode(ErrorDefine.NO_PERMISSION);
        }
        // 未知用户
        else if (e instanceof UnknownAccountException) {
            result.setRespMsg(ErrorDefine.UNKNOWN_ACCOUNT_MSG);
            result.setRespCode(ErrorDefine.UNKNOWN_ACCOUNT);
        }
        // 用户被锁定
        else if (e instanceof DisabledAccountException) {
            result.setRespMsg(ErrorDefine.DISABLE_ACCOUNT_MSG);
            result.setRespCode(ErrorDefine.DISABLE_ACCOUNT);
        }
        // 密码错误
        else if (e instanceof IncorrectCredentialsException) {
            result.setRespMsg(ErrorDefine.WRONG_PASSWORD_MSG);
            result.setRespCode(ErrorDefine.WRONG_PASSWORD);
        }
        // 未知错误
        else {
            logger.error(pjp.getSignature() + " error ", e);
            // 未知的异常，应该格外注意，可以发送邮件通知等
            result.setRespMsg(Constant.ERROR_RESP_MSG);
            result.setRespCode(Constant.ERROR_RESP_CODE);
        }
        return result;
    }
}
